package com.bhs.sansonglogistics.ui.mine;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.bhs.sansonglogistics.R;
import com.bhs.sansonglogistics.base.BaseActivity;
import com.bhs.sansonglogistics.bean.BillDataBean;
import com.bhs.sansonglogistics.bean.FeedbackRecordingBean;
import com.bhs.sansonglogistics.bean.FeedbackRecordingData;
import com.bhs.sansonglogistics.net.NetCallBack;
import com.bhs.sansonglogistics.ui.adapter.PictureShowAdapter;
import com.bhs.sansonglogistics.ui.adapter.PictureShowAdapter3;
import com.bhs.sansonglogistics.utils.DateUtils;
import com.bhs.sansonglogistics.utils.ToastUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.util.SmartGlideImageLoader;

import java.util.Arrays;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 反馈记录
 */
public class FeedbackRecordingActivity extends BaseActivity implements NetCallBack {
    private SwipeRefreshLayout mRefreshLayout;
    private RecyclerView mRvList;
    private int page = 1;
    private BaseQuickAdapter<FeedbackRecordingBean, BaseViewHolder> mAdapter;

    @Override
    public int initLayout() {
        return R.layout.activity_feedback_recording;
    }

    @Override
    public void initView() {

        TextView mTvTitle = findViewById(R.id.tv_title);
        mTvTitle.setText("反馈记录");
        mRefreshLayout = findViewById(R.id.refreshLayout);
        mRvList = findViewById(R.id.rv_list);
        mAdapter = new BaseQuickAdapter<FeedbackRecordingBean, BaseViewHolder>(R.layout.item_feedback_recording) {
            @Override
            protected void convert(@NonNull BaseViewHolder helper, FeedbackRecordingBean item) {
                helper.setText(R.id.tv_type, item.getType());
                helper.setText(R.id.tv_content, item.getContent());
                helper.setText(R.id.tv_time, DateUtils.getTime(item.getCreate_time()));
                RecyclerView mRvList = helper.getView(R.id.rv_list);
                Object[] feePic = item.getImgs().split("\\|\\|");
                PictureShowAdapter3 pictureShowAdapter = new PictureShowAdapter3(Arrays.asList(feePic));
                mRvList.setAdapter(pictureShowAdapter);
                mRvList.setLayoutManager(new GridLayoutManager(mActivity, 3));
                pictureShowAdapter.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        new XPopup.Builder(mContext)
                                .asImageViewer(view.findViewById(R.id.iv_picture), position, Arrays.asList(feePic), null, new SmartGlideImageLoader())
                                .isShowSaveButton(false)
                                .show();
                    }
                });
            }
        };

        mRvList.setAdapter(mAdapter);
        mRvList.setLayoutManager(new LinearLayoutManager(mActivity));
    }

    @Override
    public void initData() {
        loadingPopupShow();
        loadData();
        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                loadData();
            }
        });

        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                page++;
                loadData();
            }
        }, mRvList);
        mAdapter.setEmptyView(LayoutInflater.from(mActivity).inflate(R.layout.layout_empty, null));
    }

    private void loadData() {
        networkRequest(netApi.get_advice_list(page, 10, "logistics"), this);
    }

    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        FeedbackRecordingData bean = new Gson().fromJson(json, FeedbackRecordingData.class);
        mRefreshLayout.setRefreshing(false);
        loadingPopupDismiss();
        if (bean.isStatus()) {
            if (page == 1) {
                mAdapter.setNewData(bean.getData().getList());
            } else {
                mAdapter.loadMoreComplete();
                mAdapter.addData(bean.getData().getList());
            }
            if (mAdapter.getData().size() >= bean.getData().getTotal_count()) {
                mAdapter.loadMoreEnd();
            }
        } else {
            ToastUtil.show(bean.getMsg());
        }
    }

    @Override
    public void onFailure(int requestCode) {
        mRefreshLayout.setRefreshing(false);
        mAdapter.loadMoreComplete();
        loadingPopupDismiss();
    }
}